package com.example.mall.controller;

import com.example.mall.param.user.AddUserParam;
import com.example.mall.param.user.DeleteUserParam;
import com.example.mall.param.user.LoginParam;
import com.example.mall.util.ResultVo;
import com.example.mall.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ：Dongdequan
 * @date ：Created in 2022/5/25 13:58
 * @modified By：
 */

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;


    @ApiOperation("测试")
    @GetMapping("/hello")
    public Object hello() {
        redisTemplate.opsForValue().set("date", "test1");
//        System.out.println( redisTemplate.opsForValue().get("myname"));
        return redisTemplate.opsForValue().get("date");
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public ResultVo login(@RequestBody LoginParam param) {
        System.out.println("登录成功！"+param.toString());
        return userService.login(param.getUsername(), param.getPassword());
    }

    @PostMapping("/userList")
    public ResultVo userList(HttpServletRequest request){
        Long userId = getUserId(request.getHeader("Authorization"));
        return new ResultVo(200,"success",userId);
    }

    @PostMapping("/info")
    public ResultVo userInfo(HttpServletRequest request){
        System.out.println("token："+request.getAttribute("userId"));
        return userService.userInfo();
    }

    @PostMapping("/delete")
    public ResultVo deleteUser(HttpServletRequest request, @RequestBody DeleteUserParam param){
        Long userId = getUserId(request.getHeader("Authorization"));
        return userService.deleteUser(userId,param.getId());
    }

//    @PostMapping("/add")
//    public ResultVo addUser(@RequestBody AddUserParam param){
//
//    }
}
